package gov.nhso.prov.acc.apps.acc6;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;
import com.isud.paging.PageBean;

import gov.nhso.prov.acc.beans.Acc67ExpenseCategoryBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

public class ACC60106 extends Utility{
	public String url = "/apps/acc6/ACC60106A.jsp";

	public ACC60106(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if(job.equals("init")){
			this.clearLastRequest(request, "lastSearch");
			this.init(request);
		}else if(job.equals("add")){
			this.add(request);
		}else if(job.equals("save")){
			this.save(request);
		}else if(job.equals("edit")){
			this.edit(request);
		}else if(job.equals("search")){
			this.setLastRequest(request, "lastSearch");
			this.search(request);
		}else if(job.equals("viewUpdate")){
			this.viewUpdate(request);
		}else if(job.equals("back")){
			if (this.getLastRequest(request, "lastSearch")) {
				this.search(request);
			} else {
				this.init(request);
			}
		}
	}

	private String init(HttpServletRequest request){
		this.initPage(request);
		return this.url;
	}
	private String add(HttpServletRequest request){
		Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
		expenseCategoryBean.setCategoryId("สร้างจากระบบ");
		request.setAttribute("expenseCategoryBean", expenseCategoryBean);
		request.setAttribute("job", "save");
		request.setAttribute("accType", this.properties().accType());
		this.url = "/apps/acc6/ACC60106B.jsp";
		return this.url;
	}
	private String save(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		boolean success = false;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			
			String categoryName = this.getAttr(request, "categoryName");
			String categoryDesc = this.getAttr(request, "categoryDesc");
			String status = this.getAttr(request, "status");
			String categoryId = this.genCode(dao).getExpenseCategoryId();
			
			expenseCategoryBean.setCategoryId(categoryId);
			expenseCategoryBean.setCategoryName(categoryName);
			expenseCategoryBean.setCategoryAcc("6");
			expenseCategoryBean.setCategoryApply("Y");
			expenseCategoryBean.setCategorySeq("1");
			expenseCategoryBean.setActive(status);
			expenseCategoryBean.setCategoryLevel("1");
			expenseCategoryBean.setCategoryDesc(categoryDesc);
			expenseCategoryBean.setCreatedBy(this.user(request).getUserId());
			expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
			
			success = dao.insert(expenseCategoryBean);
			if(success){
				this.message(request).setMsgCode("I002");
				request.setAttribute("categoryId", categoryId);
			}else{
				this.message(request).setMsgCode("W001");
			}
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				if (success) {
					this.viewUpdate(request);
				} else {
					this.url = "/apps/acc6/ACC60106B.jsp";
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String viewUpdate(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			
			String categoryId = this.getAttr(request, "categoryId");
			expenseCategoryBean.setCategoryId(categoryId);

			expenseCategoryBean = (Acc67ExpenseCategoryBean) dao.select(expenseCategoryBean);
			request.setAttribute("job", "edit");
			request.setAttribute("accType", this.properties().accType());
			request.setAttribute("expenseCategoryBean", expenseCategoryBean);
			
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc6/ACC60106B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String edit(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		boolean success = false;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			
			String categoryName = this.getAttr(request, "categoryName");
			String categoryDesc = this.getAttr(request, "categoryDesc");
			String categoryId = this.getAttr(request, "categoryId");
			String status = this.getAttr(request, "status");
			
			expenseCategoryBean.setCategoryId(categoryId);
			expenseCategoryBean.setCategoryName(categoryName);
			expenseCategoryBean.setCategoryDesc(categoryDesc);
			expenseCategoryBean.setActive(status);
			expenseCategoryBean.setUpdatedDate(this.sysDate);
			expenseCategoryBean.setUpdatedBy(this.user(request).getUserId());
			
			success = dao.update(expenseCategoryBean);
			if(success){
				this.message(request).setMsgCode("I002");
				request.setAttribute("categoryId", categoryId);
			}else{
				this.message(request).setMsgCode("W001");
			}
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				if (success) {
					this.viewUpdate(request);
				} else {
					this.url = "/apps/acc6/ACC60106B.jsp";
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String search(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			Acc67ExpenseCategoryBean expenseCategoryBean = new Acc67ExpenseCategoryBean();
			PageBean pageBean = new PageBean();
			String categoryName = this.getAttr(request, "categoryName");
			expenseCategoryBean.setCategoryName("%" + categoryName + "%");
			expenseCategoryBean.setCategoryAcc("6");

			this.beanUtil().copyRequest2Bean(request, pageBean);
			pageBean = dao.selectPage(expenseCategoryBean, "", pageBean);
			if (pageBean.getPageData() == null) {
				this.message(request).setMsgCode("I001");
			}
			request.setAttribute("pageBean", pageBean);
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
